package com.feiyu.admin.controller;

import java.util.*;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.feiyu.admin.entity.DictGoodsEntity;
import com.feiyu.admin.entity.ShopEntity;
import com.feiyu.admin.service.ShopService;
import com.feiyu.admin.service.openFeign.ShopsAndGoodsFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.feiyu.admin.entity.GoodsEntity;
import com.feiyu.admin.service.GoodsService;
import com.feiyu.common.utils.PageUtils;
import com.feiyu.common.utils.R;


/**
 * @author wanghanbin
 * @email 1336891708@qq.com
 * @date 2021-04-08 22:35:19
 */
@RestController
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ShopsAndGoodsFeignService shopsAndGoodsFeignService;

    @Autowired
    private ShopService shopService;

    /**
     * 商品推荐优先级设置
     */
    @PostMapping("/setGoodsLv")
    //@RequiresPermissions("product:goods:update")
    public R setGoodsLv(@RequestBody GoodsEntity goods) {
        goodsService.updateById(goods);
        return R.ok("成功").put("data", null);
    }

    /**
     * 商品信息精确查询
     */
    @PostMapping("/getGoodsInfoById")
    public R getGoodsInfoById(@RequestBody GoodsEntity goodsEntity) {
        GoodsEntity goods = goodsService.getById(goodsEntity.getGoodsId());
        if (goods == null) {
            return R.error("未查询到对应商品").put("data", null);
        }
        HashMap<String, Object> goodsInfo = new HashMap<>();
        HashMap<String, Object> goodInfoImages = new HashMap<>();
        String goodsImages = goods.getGoodsImages();
        String[] split = goodsImages.split(";");
        String url = "https://test-wanghanbin.oss-cn-hangzhou.aliyuncs.com/feiyuImages/";
        for (int i = 0; i < split.length; i++) {
            goodInfoImages.put("goodImage_" + (i + 1), url + split[i]);
        }
        Integer tagId = goods.getGoodsTagId();
        DictGoodsEntity dictGoodsEntity = new DictGoodsEntity();
        dictGoodsEntity.setDictGoodsId(tagId);
        R goodsTag = shopsAndGoodsFeignService.getGoodsTagNameByTagId(dictGoodsEntity);
        Map<String, Object> data = (Map<String, Object>) goodsTag.get("data");
        Integer shopId = goods.getShopId();
        ShopEntity shopEntity = shopService.getById(shopId);
        goodsInfo.put("goodsId", goods.getGoodsId());
        goodsInfo.put("goodsName", goods.getGoodsName());
        goodsInfo.put("goodsIntro", goods.getGoodsIntro());
        goodsInfo.put("shopId", goods.getShopId());
        goodsInfo.put("shopName", shopEntity.getShopName());
        goodsInfo.put("goodsImages", goodInfoImages);
        goodsInfo.put("goodsLicense", goods.getGoodsLicense());
        goodsInfo.put("goodsRent", goods.getGoodsRent());
        goodsInfo.put("goodsDeposit", goods.getGoodsDeposit());
        goodsInfo.put("goodsPrice", goods.getGoodsPrice());
        goodsInfo.put("goodsTagId", goods.getGoodsTagId());
        goodsInfo.put("goodsTagName", data.get("dictGoodsLable"));
        goodsInfo.put("goodsTimeMode", goods.getGoodsTimeMode());
        goodsInfo.put("isAllowedBuy", goods.getIsAllowedBuy());
        goodsInfo.put("goodsPostageMode", goods.getGoodsPostageMode());
        goodsInfo.put("goodsMaxRenewalTime", goods.getGoodsMaxRenewalTime());
        goodsInfo.put("goodsNums", goods.getGoodsNums());
        goodsInfo.put("goodsRecommendLv", goods.getGoodsRecommendLv());
        goodsInfo.put("goodsState", goods.getGoodsState());
        return R.ok("操作成功").put("data", goodsInfo);
    }

    /**
     * 商品信息带参查询
     */
    @PostMapping("/getGoodsInfoByParam")
    public R getGoodsInfoByParam(@RequestBody Map<String,Object> params){
        PageUtils page = goodsService.queryPage(params);
        if (page.getList().size() == 0) {
            return R.error("未查询到相关商品名的商品").put("data", null);
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("totalCount", page.getTotalCount());
        hashMap.put("pageSize", page.getPageSize());
        hashMap.put("totalPage", page.getTotalPage());
        hashMap.put("currPage", page.getCurrPage());
        List<?> pageList = page.getList();
        List<Map<String, Object>> goodInfo_list = new ArrayList<>();
        for (int i = 0; i < pageList.size(); i++) {
            HashMap<String, Object> goodInfoImages = new HashMap<>();
            HashMap<String, Object> goodsInfo = new HashMap<>();
            GoodsEntity goods = (GoodsEntity) pageList.get(i);
            String goodsImages = goods.getGoodsImages();
            String[] split = goodsImages.split(";");
            String url = "https://test-wanghanbin.oss-cn-hangzhou.aliyuncs.com/feiyuImages/";
            for (int j = 0; j < split.length; j++) {
                goodInfoImages.put("goodImage_" + (j + 1), url + split[j]);
            }
            Integer tagId = goods.getGoodsTagId();
            DictGoodsEntity dictGoodsEntity = new DictGoodsEntity();
            dictGoodsEntity.setDictGoodsId(tagId);
            R goodsTag = shopsAndGoodsFeignService.getGoodsTagNameByTagId(dictGoodsEntity);
            Map<String, Object> data = (Map<String, Object>) goodsTag.get("data");
            Integer shopId = goods.getShopId();
            ShopEntity shopEntity = shopService.getById(shopId);
            goodsInfo.put("goodsId", goods.getGoodsId());
            goodsInfo.put("goodsName", goods.getGoodsName());
            goodsInfo.put("goodsIntro", goods.getGoodsIntro());
            goodsInfo.put("shopId", goods.getShopId());
            goodsInfo.put("shopName", shopEntity.getShopName());
            goodsInfo.put("goodsImages", goodInfoImages);
            goodsInfo.put("goodsLicense", goods.getGoodsLicense());
            goodsInfo.put("goodsRent", goods.getGoodsRent());
            goodsInfo.put("goodsDeposit", goods.getGoodsDeposit());
            goodsInfo.put("goodsPrice", goods.getGoodsPrice());
            goodsInfo.put("goodsTagId", goods.getGoodsTagId());
            goodsInfo.put("goodsTagName", data.get("dictGoodsLable"));
            goodsInfo.put("goodsTimeMode", goods.getGoodsTimeMode());
            goodsInfo.put("isAllowedBuy", goods.getIsAllowedBuy());
            goodsInfo.put("goodsPostageMode", goods.getGoodsPostageMode());
            goodsInfo.put("goodsMaxRenewalTime", goods.getGoodsMaxRenewalTime());
            goodsInfo.put("goodsNums", goods.getGoodsNums());
            goodsInfo.put("goodsRecommendLv", goods.getGoodsRecommendLv());
            goodsInfo.put("goodsState", goods.getGoodsState());
            goodInfo_list.add(goodsInfo);
        }
        hashMap.put("list", goodInfo_list);
        return R.ok("成功").put("data",hashMap);
    }
}
